package com.hedd.shop.controller;

import com.github.pagehelper.PageInfo;
import com.hedd.shop.constants.Constants;
import com.hedd.shop.pojo.entity.GoodInfo;
import com.hedd.shop.pojo.entity.User;
import com.hedd.shop.pojo.entity.UserInfo;
import com.hedd.shop.pojo.receptparms.UserUpdateQo;
import com.hedd.shop.pojo.vo.RespResult;
import com.hedd.shop.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/userInfo")
@Api(tags = "用户信息接口")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    //返回结果将被装换为json
    @ResponseBody
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    @ApiOperation(value = "根据ID获取信息")

    public RespResult info(HttpServletRequest request) {
        HttpSession session1 = request.getSession();
        User loginUser =(User) session1.getAttribute(Constants.SESSION_KEY_LOGIN_USER);

        UserInfo userInfo = userInfoService.userInfo(loginUser.getUserId());
        RespResult<UserInfo> respResult = new RespResult<>();
        respResult.setCode(RespResult.Code.SUCCESS.getValue());
        respResult.setData(userInfo);
        return  respResult;
    }


    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "测试传递对象过来",notes = "调用当前方法的注意事项")
    public  RespResult saveFile( UserUpdateQo userUpdateQo,
                                    @RequestParam(value = "birthday_String",required = false) String birthday_String,
                                    @RequestParam(value="file",required=false) MultipartFile file,
                                HttpServletRequest request) throws IOException, ParseException {

        HttpSession session1 = request.getSession();
        User loginUser =(User) session1.getAttribute(Constants.SESSION_KEY_LOGIN_USER);
        userUpdateQo.setId(loginUser.getUserId());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date birthday = simpleDateFormat.parse(birthday_String);
        userUpdateQo.setBirthday(birthday);
        //接收表单提交的数据,包含文件
        int updateResp = userInfoService.updateUserInfoById(userUpdateQo, file);
        RespResult<Object> respResult = new RespResult<>();
        respResult.setCode(updateResp);
        return respResult;
    }
}
